using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Pitoshka.Tools.Helpers
{
	public class ImageHelper
	{
		/// <summary>
		/// Returns image force resized to MaxWidth
		/// </summary>
		/// <param name="imagepath"></param>
		/// <param name="MaxWidth"></param>
		/// <returns></returns>
		public static Image ResizeFromWidth(string imagepath, int MaxWidth)
		{
			System.Drawing.Image oImg = System.Drawing.Image.FromFile(imagepath);
			int oWidth = oImg.Width;
			int oHeight = oImg.Height;
			int tWidth = MaxWidth;
			int tHeight = oHeight * (MaxWidth / oWidth);

			System.Drawing.Image oThumbNail = new Bitmap(tWidth, tHeight, oImg.PixelFormat);

			Graphics oGraphic = Graphics.FromImage(oThumbNail);
			oGraphic.CompositingQuality = CompositingQuality.HighQuality;
			oGraphic.SmoothingMode = SmoothingMode.HighQuality;
			oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

			Rectangle oRectangle = new Rectangle(0, 0, tWidth, tHeight);
			oGraphic.DrawImage(oImg, oRectangle);
			oImg.Dispose();
			return oThumbNail;
		}

		/// <summary>
		/// Returns image resized to MaxWidth or less
		/// </summary>
		/// <param name="imagepath"></param>
		/// <param name="MaxWidth"></param>
		/// <returns></returns>
		public static Image ResizeFromMaxWidth(string imagepath, int MaxWidth)
		{
			Image oImg = Image.FromFile(imagepath);
			return ResizeFromMaxWidth(oImg, MaxWidth);
		}
		public static Image ResizeFromMaxWidth(Image oImg, int MaxWidth)
		{
			int oWidth = oImg.Width;
			int oHeight = oImg.Height;

			int tWidth = MaxWidth;
			int tHeight = (int)(((double)oHeight * ((double)MaxWidth / (double)oWidth)));

			if (oImg.Width <= MaxWidth)
			{
				Image oThumbNail = new Bitmap(tWidth, tHeight); //, oImg.PixelFormat
				Brush backBrush = new SolidBrush(Color.Black);

				Graphics oGraphic = Graphics.FromImage(oThumbNail);
				oGraphic.CompositingQuality = CompositingQuality.HighQuality;
				oGraphic.SmoothingMode = SmoothingMode.HighQuality;
				oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
				

				Rectangle oRectangle = new Rectangle(0, 0, tWidth, tHeight);

				//Pen p = new Pen(Color.Black, 1) ;
				//oGraphic.DrawRectangle(p, oRectangle); 
				oGraphic.FillRectangle(backBrush, oRectangle);


				oGraphic.DrawImage(oImg,
					(tWidth - oWidth) / 2,
					(tHeight - oHeight) / 2,
					oImg.Width,
					oImg.Height);
				oImg.Dispose();
				return oThumbNail;


				// oImg.Dispose(); 
				// return oImg;
			}
			else
			{
				Image oThumbNail = new Bitmap(tWidth, tHeight); //, oImg.PixelFormat

				Graphics oGraphic = Graphics.FromImage(oThumbNail);
				oGraphic.CompositingQuality = CompositingQuality.HighQuality;
				oGraphic.SmoothingMode = SmoothingMode.HighQuality;
				oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

				Rectangle oRectangle = new Rectangle(0, 0, tWidth, tHeight);
				oGraphic.DrawImage(oImg, oRectangle);
				oImg.Dispose();
				return oThumbNail;
			}
		}
		/// <summary>
		/// Returns image resized to MaxWidth or less
		/// </summary>
		/// <param name="imagepath"></param>
		/// <param name="MaxWidth"></param>
		/// <returns></returns>
		public static Image ResizeFromMaxHeight(string imagepath, int MaxHeight)
		{
			Image oImg = Image.FromFile(imagepath);
			return ResizeFromMaxHeight(oImg, MaxHeight);
		}
		public static Image ResizeFromMaxHeight(Image oImg, int MaxHeight)
		{			
			int oWidth = oImg.Width;
			int oHeight = oImg.Height;

			int tWidth = (int)(((double)oWidth * ((double)MaxHeight / (double)oHeight)));
			int tHeight = MaxHeight;

			if (oImg.Height <= MaxHeight)
			{
				Image oThumbNail = new Bitmap(tWidth, tHeight); //, oImg.PixelFormat
				Brush backBrush = new SolidBrush(Color.Black);

				Graphics oGraphic = Graphics.FromImage(oThumbNail);
				oGraphic.CompositingQuality = CompositingQuality.HighQuality;
				oGraphic.SmoothingMode = SmoothingMode.HighQuality;
				oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

				Rectangle oRectangle = new Rectangle(0, 0, tWidth, tHeight);

				//Pen p = new Pen(Color.Black, 1) ;
				//oGraphic.DrawRectangle(p, oRectangle); 
				oGraphic.FillRectangle(backBrush, oRectangle);


				oGraphic.DrawImage(oImg,
					(tWidth - oWidth) / 2,
					(tHeight - oHeight) / 2,
					oImg.Width,
					oImg.Height);
				oImg.Dispose();
				return oThumbNail;


				// oImg.Dispose(); 
				// return oImg;
			}
			else
			{
				Image oThumbNail = new Bitmap(tWidth, tHeight); //, oImg.PixelFormat

				Graphics oGraphic = Graphics.FromImage(oThumbNail);
				oGraphic.CompositingQuality = CompositingQuality.HighQuality;
				oGraphic.SmoothingMode = SmoothingMode.HighQuality;
				oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;

				Rectangle oRectangle = new Rectangle(0, 0, tWidth, tHeight);
				oGraphic.DrawImage(oImg, oRectangle);
				oImg.Dispose();
				return oThumbNail;
			}
		}

		public static Image ResizeFromMaxSize(string imagepath, int MaxWidth, int MaxHeight)
		{
			Image oImg = Image.FromFile(imagepath);

			decimal ideal = MaxHeight / MaxWidth;
			decimal real = oImg.Height / oImg.Width;

			if (oImg.Height > MaxHeight || oImg.Width > MaxWidth)
			{
				if (real < ideal)
				{
					return ResizeFromMaxWidth(oImg, MaxWidth);
				}
				else
				{
					return ResizeFromMaxHeight(oImg, MaxHeight);
				}
			}
			else
			{
				return oImg;
			}
		}
	}
}
